package com.bwc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwc.common.BussinessException;
import com.bwc.dao.CategoryDao;
import com.bwc.domain.Category;
import com.bwc.domain.Dish;
import com.bwc.domain.Setmeal;
import com.bwc.service.CategoryService;
import com.bwc.service.DishService;
import com.bwc.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;

    @Override
    public void remove(Long id) throws BussinessException{

        //dish中根据categoryId查出来的记录不为0，则说明从菜品分类有属于它的菜品
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper();
        dishWrapper.eq(Dish::getCategoryId,id);

        int count1 =  dishService.count(dishWrapper);
        if(count1 > 0) {
            //抛出业务异常
            throw new BussinessException("当前分类下光联了菜品，不能删除");
        }


        //setmeal中根据categoryId查出来的记录不为0，则说明从套餐分类有属于它的套餐
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper();
        setmealWrapper.eq(Setmeal::getCategoryId,id);

        int count2 =  setmealService.count(setmealWrapper);
        if(count2 > 0){
            //抛出业务异常
            throw new BussinessException("当前分类下关联了套餐，不能删除");
        }

        super.removeById(id);
    }

//    public void remove(Long id) throws BussinessException {
//
//        //dish中根据categoryId查出来的记录不为0，则说明从菜品分类有属于它的菜品
//        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper();
//        dishWrapper.eq(Dish::getCategoryId,id);
//
//        int count1 =  dishService.count(dishWrapper);
//        if(count1 > 0) {
//            //抛出业务异常
//            throw new BussinessException("当前分类下光联了菜品，不能删除");
//        }
//
//
//        //setmeal中根据categoryId查出来的记录不为0，则说明从套餐分类有属于它的套餐
//        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper();
//        setmealWrapper.eq(Setmeal::getCategoryId,id);
//
//        int count2 =  dishService.count(dishWrapper);
//        if(count1 > 0){
//            //抛出业务异常
//            throw new BussinessException("当前分类下关联了套餐，不能删除");
//        }
//
//        super.removeById(id);
//    }

}
